package xhwl.insurance.customView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.List;

import xhwl.insurance.R;
import xhwl.insurance.util.ToastUtil;


/**
 * @author admin
 *
 */
public class PullToRefreshListView extends RelativeLayout implements PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {
	PullToRefreshView mPullToRefreshView;
	ListView mListView;
	LinearLayout emptyView;
	ImageView nodateImg;
	int page = 1, pullState = PullToRefreshView.PullState.PULL_DOWN;
	private RefreshListener callback;
	
	BaseAdapter adapter;
	Context mContext;
	public PullToRefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext=context;
		LayoutInflater.from(context).inflate(R.layout.pulltorefreshlistview, this);
		initView();
		initListener();
	}
	private void initView(){
		emptyView = (LinearLayout) findViewById(R.id.emptyView);
		nodateImg=(ImageView) findViewById(R.id.nodata);
		mPullToRefreshView = (PullToRefreshView)findViewById(R.id.pullToRefreshView);
		mListView = (ListView) findViewById(R.id.mListView);
	}
	private void initListener(){
		mPullToRefreshView.setOnFooterRefreshListener(this);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
	}
	public void setAdapter(BaseAdapter adapter){
		if(adapter==null)
			return;
		this.adapter=adapter;
		mListView.setAdapter(adapter);
	}
	public void setOnItemClickListener(OnItemClickListener listener){
		if(listener==null)
			return;
		mListView.setOnItemClickListener(listener);
	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		pullState = PullToRefreshView.PullState.PULL_DOWN;
		if(callback!=null)
		{
			callback.onRefresh(1);
			mPullToRefreshView.onHeaderRefreshComplete();
		}else {
			mPullToRefreshView.onHeaderRefreshComplete();
		}
	}
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		pullState = PullToRefreshView.PullState.PULL_UP;
		if(callback!=null)
		{
			callback.onRefresh(page+1);
			mPullToRefreshView.onFooterRefreshComplete();
		}else {
			mPullToRefreshView.onFooterRefreshComplete();
		}
	}
	public interface RefreshListener{
		public void onRefresh(int page);
	}
	
	public void setRefreshListener(RefreshListener callback){
		this.callback=callback;
	}
	public interface Callback{
		void onRefreshBtnClick(View view);
	}
	/**
	 * 请求成功时调用
	 * @param list data数据集合
	 * @param tempListSize 获取的临时列表数据size
	 */
	public void onRefreshComplete(List<? extends Object> list , int tempListSize){
		if (pullState == PullToRefreshView.PullState.PULL_DOWN) {
			mPullToRefreshView.onHeaderRefreshComplete();
			page=1;
			if(list!=null)
			{
				list.clear();
			}
			if(tempListSize==0){
				emptyView.setVisibility(View.VISIBLE);
			}
			else
			{
				emptyView.setVisibility(View.GONE);
			}
		} else {
			mPullToRefreshView.onFooterRefreshComplete();
			if(tempListSize==0){
				ToastUtil.showToast(mContext, mContext.getString(R.string.no_more_data));
			}
			else
			{
				page=page+1;
			}
		   
		}
	}
	/**
	 * 请求成功时调用
	 * @param list data数据集合
	 * @param tempListSize 获取的临时列表数据size
	 * @param drawableId 无数据时显示的图片
	 */
	public void onRefreshComplete(List<? extends Object> list , int tempListSize, int drawableId){
		if (pullState == PullToRefreshView.PullState.PULL_DOWN) {
			mPullToRefreshView.onHeaderRefreshComplete();
			page=1;
			if(list!=null)
			{
				list.clear();
			}
			if(tempListSize==0){
				emptyView.setVisibility(View.VISIBLE);
				if(drawableId>0){
					nodateImg.setImageResource(drawableId);
				}
			}
			else
			{
				emptyView.setVisibility(View.GONE);
			}
		} else {
			mPullToRefreshView.onFooterRefreshComplete();
			if(tempListSize==0){
				ToastUtil.showToast(mContext, mContext.getString(R.string.no_more_data));
			}
			else
			{
				page=page+1;
			}
		   
		}
	}
	/**
	 * 请求失败是调用
	 */
	public void onRefreshComplete(){
		if (pullState == PullToRefreshView.PullState.PULL_DOWN) {
			mPullToRefreshView.onHeaderRefreshComplete();
		} else {
			mPullToRefreshView.onFooterRefreshComplete();
		}
	}
	public boolean isPullDown(){
		return pullState== PullToRefreshView.PullState.PULL_DOWN;
	}
	public boolean isEnablePullTorefresh() {
		return mPullToRefreshView.isEnablePullTorefresh();
	}

	public void setEnablePullTorefresh(boolean enablePullTorefresh) {
		mPullToRefreshView.setEnablePullTorefresh(enablePullTorefresh);
	}

	public boolean isEnablePullLoadMoreDataStatus() {
		return mPullToRefreshView.isEnablePullLoadMoreDataStatus();
	}

	public void setEnablePullLoadMoreDataStatus(
			boolean enablePullLoadMoreDataStatus) {
		mPullToRefreshView.setEnablePullLoadMoreDataStatus(enablePullLoadMoreDataStatus);
	}
}
